package cn.edu.scau.cmi.wujiaxian.comprehensive.service;

import java.util.ArrayList;

import cn.edu.scau.cmi.wujiaxian.comprehensive.dao.ClazzDao;
import cn.edu.scau.cmi.wujiaxian.comprehensive.dao.ScoreDao;
import cn.edu.scau.cmi.wujiaxian.comprehensive.dao.StudentDao;
import cn.edu.scau.cmi.wujiaxian.comprehensive.model.Clazz;
import cn.edu.scau.cmi.wujiaxian.comprehensive.model.Score;
import cn.edu.scau.cmi.wujiaxian.comprehensive.model.ScoreInterval;
import cn.edu.scau.cmi.wujiaxian.comprehensive.model.Student;

public class StudentService {
	
	public static void main(String[] args) {
		StudentService studentService = new StudentService();
//		studentService.addStudent("蓝俊豪", "201734020121", "2017级软件工程2班");
//		studentService.deleteStudent("201734020121");
		System.out.println(studentService.selectStudents(1, "SX001"));
	}
	
	/**
	 * 增加一个学生
	 * @param name
	 * @param studentNumber
	 * @param graceAndMajorAndClazz
	 */
	public void addStudent(String name, String studentNumber, String graceAndMajorAndClazz) {
		ClazzDao clazzDao = new ClazzDao();
		ArrayList<Clazz> clazzs = clazzDao.select("graceAndMajorAndClazz", graceAndMajorAndClazz);
		Student student = new Student();
		student.setName(name);
		student.setStudentNumber(studentNumber);
		student.setClazzId(clazzs.get(0).getId());
		
		StudentDao studentDao = new StudentDao();
		studentDao.add(student);
	}
	
	/**
	 * 删除一个学生信息
	 * @param studentNumber
	 */
	public void deleteStudent(String studentNumber) {
		StudentDao studentDao = new StudentDao();
		studentDao.delete("studentNumber", studentNumber);
	}
	
	/**
	 * 搜索该班级该课程的所有学生信息
	 * @param clazzId
	 * @param courseNumber
	 * @return
	 */
	public ArrayList<Student> selectStudents(Integer clazzId, String courseNumber) {
		StudentDao studentDao = new StudentDao();
		ArrayList<Student> students = studentDao.select("clazzId", clazzId);
		for (Student student : students) {
			ScoreDao scoreDao = new ScoreDao();
			ArrayList<Score> scores = scoreDao.select("studentNumber", student.getStudentNumber());
			for (Score score : scores) {
				if (score.getCourseNumber().equals(courseNumber)) {
					student.setScore(score);
				}
			}
		}
		return students;
	}
	
	/**
	 * 返回各个成绩区间的人数和百分比
	 * @param students
	 * @return
	 */
	public ArrayList<ScoreInterval> getScoreIntervals(ArrayList<Student> students) {
		String[] intervals = {"90-100","80-89","70-79","60-69","0-59"};
		ArrayList<ScoreInterval> scoreIntervals = new ArrayList<>();
		for (int i = 0; i < intervals.length; i++) {
			ScoreInterval scoreInterval = new ScoreInterval();
			scoreInterval.setScoreInterval(intervals[i]);
			scoreInterval.setNumber(0);
			scoreIntervals.add(scoreInterval);
		}
		for (Student student : students) {
			Integer totalalScore = student.getScore().getTotalScore();
			if (totalalScore >= 90 && totalalScore <= 100) {
				scoreIntervals.get(0).setNumber(scoreIntervals.get(0).getNumber() + 1);
			} else if (totalalScore >= 80 && totalalScore <= 89) {
				scoreIntervals.get(1).setNumber(scoreIntervals.get(1).getNumber() + 1);
			} else if (totalalScore >= 70 && totalalScore <= 79) {
				scoreIntervals.get(2).setNumber(scoreIntervals.get(2).getNumber() + 1);
			} else if (totalalScore >= 60 && totalalScore <= 69) {
				scoreIntervals.get(3).setNumber(scoreIntervals.get(3).getNumber() + 1);
			} else {
				scoreIntervals.get(4).setNumber(scoreIntervals.get(4).getNumber() + 1);
			}
		}
		for (int i = 0; i < intervals.length; i++) {
			int number = students.size();
			ScoreInterval scoreInterval = scoreIntervals.get(i);
			Double percentage =  (scoreInterval.getNumber() / (double) number);
			scoreInterval.setPercentage(percentage);
		}
 		return scoreIntervals;
	}
	
}
